package com.xindz.cn.mvptest.components.product.model;

import android.os.Build;
import android.support.annotation.RequiresApi;

import com.avos.avoscloud.AVObject;
import com.xindz.cn.mvptest.annotation.LeanCloudClass;
import com.xindz.cn.mvptest.annotation.LeanCloudClassField;

import java.lang.reflect.Field;

/**
 * @name MvpTest
 * @class name：com.xindz.cn.mvptest.components.product.model
 * @describe describe
 * @anthor XINDZ
 * @time 2017/11/2 17:00
 * @change
 * @chang time
 * @class describe
 */
public class LeanCloudModel<T,B extends AVObject> {

    @RequiresApi(api = Build.VERSION_CODES.N)
    public String save(T entity) throws Exception {
        String objectId = "foo";

        Class<?> clazz = entity.getClass();
        LeanCloudClass leanCloudClass = clazz.getDeclaredAnnotation(LeanCloudClass.class);
        String leanCloudClassName = leanCloudClass.value();
        if ("".equals(leanCloudClassName)) {
            leanCloudClassName = clazz.getSimpleName();
        }

        AVObject avObject = null;
        Field[] fields = entity.getClass().getDeclaredFields();
        for (Field field : fields) {
            LeanCloudClassField leanCloudClassField = field.getDeclaredAnnotation(LeanCloudClassField.class);
            if (leanCloudClassField == null) {
                continue;
            }
            String leanCloudClassFieldName = leanCloudClassField.value();
            if ("".equals(leanCloudClassFieldName)) {
                leanCloudClassFieldName = field.getName();
            }

            if (avObject == null) {
                avObject = new AVObject(leanCloudClassName);
            }
            field.setAccessible(true);
            avObject.put(leanCloudClassFieldName, field.get(entity));
        }

        if (avObject != null) {
            avObject.save();
            objectId = avObject.getObjectId();
        }

        return objectId;
    }
    @RequiresApi(api = Build.VERSION_CODES.N)
    public String fetch(String keys,T entity) throws Exception {
        String objectId = "foo";

        Class<?> clazz = entity.getClass();
        LeanCloudClass leanCloudClass = clazz.getDeclaredAnnotation(LeanCloudClass.class);
        String leanCloudClassName = leanCloudClass.value();
        if ("".equals(leanCloudClassName)) {
            leanCloudClassName = clazz.getSimpleName();
        }

        AVObject avObject = null;
        Field[] fields = entity.getClass().getDeclaredFields();
        for (Field field : fields) {
            LeanCloudClassField leanCloudClassField = field.getDeclaredAnnotation(LeanCloudClassField.class);
            if (leanCloudClassField == null) {
                continue;
            }
            String leanCloudClassFieldName = leanCloudClassField.value();
            if ("".equals(leanCloudClassFieldName)) {
                leanCloudClassFieldName = field.getName();
            }

            if (avObject == null) {
                avObject = new AVObject(leanCloudClassName);
            }
            field.setAccessible(true);
            avObject.put(leanCloudClassFieldName, field.get(entity));
        }

        if (avObject != null) {
            avObject.fetch(keys);
            objectId = avObject.getObjectId();
        }
        return objectId;
    }
    @RequiresApi(api = Build.VERSION_CODES.N)
    public String delete(T entity) throws Exception {
        String objectId = "foo";

        Class<?> clazz = entity.getClass();
        LeanCloudClass leanCloudClass = clazz.getDeclaredAnnotation(LeanCloudClass.class);
        String leanCloudClassName = leanCloudClass.value();
        if ("".equals(leanCloudClassName)) {
            leanCloudClassName = clazz.getSimpleName();
        }

        AVObject avObject = null;
        Field[] fields = entity.getClass().getDeclaredFields();
        for (Field field : fields) {
            LeanCloudClassField leanCloudClassField = field.getDeclaredAnnotation(LeanCloudClassField.class);
            if (leanCloudClassField == null) {
                continue;
            }
            String leanCloudClassFieldName = leanCloudClassField.value();
            if ("".equals(leanCloudClassFieldName)) {
                leanCloudClassFieldName = field.getName();
            }

            if (avObject == null) {
                avObject = new AVObject(leanCloudClassName);
            }
            field.setAccessible(true);
            avObject.put(leanCloudClassFieldName, field.get(entity));
        }

        if (avObject != null) {
            avObject.delete();
            objectId = avObject.getObjectId();
        }
        return objectId;
    }

}
